package com.shawn.admin.service;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shawn.admin.exception.BusinessException;
import com.shawn.admin.interceptor.TokenInterceptor;
import com.shawn.admin.mapper.AdminMapper;
import com.shawn.commons.utils.JwtUtils;
import com.shawn.commons.utils.RC;
import com.shawn.model.entity.Admin;
import com.shawn.model.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class SystemService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private TokenInterceptor tokenInterceptor;

    public void getCode(String uuid, HttpServletResponse response) throws IOException {
        //1生成验证码对象 this(width, height, 5位数, 150线条数);
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(299, 97);
        //2获取验证码
        String code = captcha.getCode();
        //验证码存入redis 根据前端传送的uuid匹配验证码
        redisTemplate.opsForValue().set(RC.CAP_CODE+uuid,code,10, TimeUnit.MINUTES);
        //输出验证码
        captcha.write(response.getOutputStream());
    }

    /**
     * 登录检查
     *
     * @param username 用户名
     * @param password 密码
     * @param code     代码
     * @param uuid     uuid
     * @return {@link Map}
     */
    public ResponseEntity loginCheck(String username, String password, String code, String uuid) {
        //获取redis验证码
        String realCode = redisTemplate.opsForValue().get(RC.CAP_CODE + uuid);
        if (StringUtils.isEmpty(code)||!code.equals(realCode)){
            throw new BusinessException(ErrorResult.codeError());
        }
        //用户名密码空判断
        if (StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            return ResponseEntity.status(500).body(new HashMap<>());
        }
        //数据正常获取同名用户
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getUsername,username);
        Admin admin = adminMapper.selectOne(wrapper);
        //密码鉴权
            String md5 = SecureUtil.md5(password);
        //将登录密码转为md5
        if (admin==null||!md5.equals(admin.getPassword())){
            //没有该用户
            throw new BusinessException(ErrorResult.notFondAdminError());
        }
        //登录成功
        //删除验证码
        redisTemplate.delete(RC.CAP_CODE+uuid);
        //构造token返回
        HashMap<String,Object> map = new HashMap<>();
        map.put("username",username);
        map.put("id",admin.getId());
        String token = JwtUtils.getToken(map);
        Map<Object, Object> res = new HashMap<>();
        res.put("token",token);
        return ResponseEntity.ok(res);
    }

    public ResponseEntity getUserInfo(Long id) {
        Admin admin = adminMapper.selectById(id);
        return ResponseEntity.ok(admin);
    }

    public void logout() {
    }
}

